#include "forgetpassword.h"
#include "ui_forgetpassword.h"

#include"smtp.h"

#include<QSqlQuery>
#include<QDebug>
#include<QMessageBox>
#include<QDateTime>

#include <qdesktopwidget.h>
#include "myqlabel.h"

#include"titlebarfix.h"

forgetPassword::forgetPassword(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::forgetPassword)
{
    //关闭窗口，即销毁窗体
    this->setAttribute(Qt::WA_DeleteOnClose, true);

    ui->setupUi(this);
    //设置lable的颜色 选中状态
    QPalette pa;
    pa.setColor(QPalette::WindowText,Qt::white);
    ui->label->setPalette(pa);
    //设置窗口图标
    this->setWindowIcon(QIcon(":/images/软件图标.png"));

    //计时器
    timer = new QTimer(this);
    connect(timer,  SIGNAL(timeout()),  this,  SLOT(run()));


    fgp2=new forgetpassword2;
    connect(this,SIGNAL(sendEmail(QString)),fgp2,SLOT(getEmail(QString)));



}

forgetPassword::~forgetPassword()
{
    delete ui;
    delete timer;
}

//生成四位验证码
QString forgetPassword::createCode()
{
   qsrand(QDateTime::currentMSecsSinceEpoch());//为随机值设定一个seed

    const char ch[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    int size = sizeof(ch);
    QString str;
    int num = 0;
    for (int i = 0; i < 4; ++i)
    {
        num = rand() % (size - 1);
        str[i] = ch[num];
    }

    return str;
}

//进入密码重置界面
void forgetPassword::on_pushButton_next_clicked()
{

    if(ui->lineEdit_code->text()==code&&ui->lineEdit_code->text()!=nullptr){
        //需要判断，数据库是否有这个邮箱存在
        QString email=ui->lineEdit_email->text();
        QSqlQuery query;
        QString sql_email="select user_name from user where email='"+email+"'";


        qDebug()<<"sql_email"<<sql_email;
        query.exec(sql_email);
        qDebug()<<QStringLiteral("找到email个数:")<<query.size();

        if(query.size()!=0){

            emit sendsignal();
            this->close();
            titlebarFix *tBar=new titlebarFix;
            connect(fgp2,SIGNAL(sendsignal()),tBar,SLOT(close()));
            tBar->setAttribute(Qt::WA_DeleteOnClose, true);
            tBar->setMainWidget(fgp2);
            tBar->setTitleBarText(QStringLiteral("密码重置"));
            tBar->show();
//            fgp2->show();

            //发送邮箱到密码重置界面
            emit sendEmail(ui->lineEdit_email->text());
        }
        else {
            QMessageBox::warning(this,QStringLiteral("找回密码失败"),QStringLiteral("很抱歉，该用户未注册"));
        }

    }
    else {
        QMessageBox::warning(this,QStringLiteral("找回密码失败"),QStringLiteral("验证码错误,请重新输入"));
    }



}
int forgetPassword::countNumber=60;
//判断是否有这个邮箱用户，如果有，则发送验证码，否则弹窗。验证码60s一次。
void forgetPassword::on_pushButton_getcode_clicked()
{
    QString email=ui->lineEdit_email->text();

    //判断是不是邮箱格式
    QRegExp rx("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");
    if(!rx.exactMatch(email))//匹配邮箱格式
    {
        QMessageBox::warning(this,QStringLiteral("邮箱发送失败"),QStringLiteral("邮箱账号格式不正确,请重新输入"));
        //        ui->lineEdit_email->setText(NULL);//清空？
    }else
    {
        //发送邮箱
        code=this->createCode();
        qDebug()<<"code:"<<code;
        Smtp s("lxc18623235839@163.com","QDOAKHZUFATSDNBR");
        QString text=QStringLiteral("结直肠息肉检测平台：验证码：")+code;
        s.send(email.toUtf8(),QStringLiteral("息肉检测平台邮箱验证"),text);
        qDebug()<<email.toUtf8();

        ui->pushButton_getcode->setDisabled(true);
        //开始60s等待时间
        timer->start(1000);
    }

}

//倒计时60s，结束重置
void forgetPassword::run()
{
    if(countNumber!=0){
        ui->pushButton_getcode->setText(QString::number(countNumber)+QStringLiteral("s后再发送"));
        countNumber--;
//        qDebug()<<countNumber;
    }
    else {
        timer->stop();
        countNumber=60;
        ui->pushButton_getcode->setText(QStringLiteral("获取验证码"));
        ui->pushButton_getcode->setDisabled(false);
    }


}


